/**
 * @description 购物车 合并相同项
 * @param {any[]} list
 */

import _ from 'lodash'
import {floatAdd,floatMul} from '@/utils/util'
 export let mergeSameItem = (list) => {
    let arr = [];
    let equalFn = (item) => {
      let hadSame = false;
      //if(!item.guid) item.guid = setGUID()
      if (item?.isWeightingSale == true) {
        //item.qty = 0;
        return item;
      }/* else if (item.type == 3 || item.type == 5) {
        item.sumCount = 1;
        return item;
      }*/
      for (let a of arr) {
        if (a.productId == item.productId) {
          if(_.isEqual(a.packages, item.packages)){
            hadSame = true;
            a.qty++;
            return false;
          }
        }
      }
      // if (hadSame == false) {
      //   item.qty = 1;
      //   return item;
      // }
    };
    list.forEach((s) => {
      let equalRes = equalFn(s);
      if (equalRes) {
        arr.push(equalRes);
      }
    });
    return arr;
  };
  /**
     * @description 生成独一无二key，避免套餐删除时，同一productId套餐出现编辑状态
     */
   export let setGUID = ()=>{
    let genStr = () => {
      let str = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
        /[xy]/g,
        function (c) {
          var r = (Math.random() * 16) | 0,
            v = c == "x" ? r : (r & 0x3) | 0x8;
          return v.toString(16);
        }
      );
      return str;
    };
    let uid = genStr();
    return uid;
  };
  /**
   * @description 计算单一产品里面的套餐与菜品价格
   */
  export let singleItemCalculate = (item, incudeInitPrice = true) => {
    let count = item.qty || 1;
    //console.log("singleItemCalculateItem", item);
    let initPrice = isNaN(item.salePrice * count) ? 0 : floatMul(item.salePrice, count);
    let defaultPrice = 0;
    /**
     * @description 套餐计算
     */
    let calcPackages = (plist, price = 0) => {
      plist.forEach((p) => {
        if(p.items){
          p.items.forEach((pitem) => {
            pitem.itemCount > 0 && (price = floatAdd(price,calcFood(pitem.foodGroups)) );
          });
        }else{
          // if(p.qty > 0){
          //   for(let i=0;i<p.qty;i++){
          //     price = floatAdd(price,calcFood(p.foodGroups))
          //   }
          // }
          p.qty > 0 && (price = floatAdd(price,calcFood(p.foodGroups)) );
        }
      });
      return price;
    };
    /**
     * @description 菜品计算
     */
    let calcFood = (flist, price = 0) => {
      flist.forEach((f) => {
        if(f.items){
          f.items.forEach((fitem) => {
            fitem.isDefault == true && (price = floatAdd(price,fitem.extraPrice));
          });
        }else{
          price = floatAdd(price,f.extraPrice)
        }
      });
      return price;
    };
    if (item.type == 3 && item.packages.length > 0) {
      if (!item?.isWeightingSale == true) {
        defaultPrice = floatAdd(defaultPrice,calcPackages(item.packages));
      }
    }
    if (item.type == 5 && item.foodGroups.length > 0) {
      if (!item?.isWeightingSale == true) {
        defaultPrice = floatAdd(defaultPrice,calcFood(item.foodGroups));
      }
    }
    
    let sum = 0
    for(let i=0;i<count;i++){
      sum = floatAdd(sum,defaultPrice)
    }
    defaultPrice = sum

    let finalPrice =
      incudeInitPrice == true ? floatAdd(defaultPrice,initPrice) : defaultPrice;
    return finalPrice;
  };
  
  /**
   *
   * @example plist=[items:[1,2,3]] => plist=[1,2,3]
   */
  //let getFlatItemArray = (list) => {};
  